package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import freemarker.template.SimpleDate;
import hl.dao.BaseDao;
import hl.model.TCoach;
import hl.model.TCourseDetail;
import hl.model.TGroupCourse;
import hl.model.TPlace;
import hl.pageModel.DataGrid;
import hl.pageModel.GroupCourse;
import hl.service.GroupCourseService;

@Service("groupCourseService")
public class GroupCourseServiceImpl implements GroupCourseService {

	private BaseDao<TGroupCourse> groupCourseDao;

	private BaseDao<TCoach> coachDao;

	private BaseDao<TCourseDetail> courseDetailDao;

	private BaseDao<TPlace> placeDao;

	public BaseDao<TPlace> getPlaceDao() {
		return placeDao;
	}

	@Autowired
	public void setPlaceDao(BaseDao<TPlace> placeDao) {
		this.placeDao = placeDao;
	}

	public BaseDao<TCourseDetail> getCourseDetailDao() {
		return courseDetailDao;
	}

	@Autowired
	public void setCourseDetailDao(BaseDao<TCourseDetail> courseDetailDao) {
		this.courseDetailDao = courseDetailDao;
	}

	public BaseDao<TGroupCourse> getGroupCourseDao() {
		return groupCourseDao;
	}

	@Autowired
	public void setgroupCourseDao(BaseDao<TGroupCourse> groupCourseDao) {
		this.groupCourseDao = groupCourseDao;
	}

	public BaseDao<TCoach> getcoachDao() {
		return coachDao;
	}

	@Autowired
	public void setcoachDao(BaseDao<TCoach> coachDao) {
		this.coachDao = coachDao;
	}

	@Override
	public DataGrid getDatagrid(GroupCourse groupCourse) {
		DataGrid dg = new DataGrid();
		String hql = "from TGroupCourse t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(groupCourse, hql, params);

		if (groupCourse.getOrder() != null) {
			hql += " order by " + groupCourse.getSort() + " " + groupCourse.getOrder();
		}

		List<TGroupCourse> l = groupCourseDao.find(hql, params, groupCourse.getPage(), groupCourse.getRows());

		int totalHql = groupCourseDao.count(hql, params);

		List<GroupCourse> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TGroupCourse t : l) {
				GroupCourse u = new GroupCourse();
				BeanUtils.copyProperties(t, u);
				u.setCoachName(t.getTCoach().getName());
				u.setPkCoach(t.getTCoach().getPkCoach());
				u.setCourseName(t.getTCourseDetail().getName());
				u.setPkCourseDetail(t.getTCourseDetail().getPkCourseDetail());
				u.setPlaceName(t.getTPlace().getName());
				u.setPkPlace(t.getTPlace().getPkPlace());
				u.setPartInCount(t.getTMembers().size());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(GroupCourse groupCourse, String hql, Map<String, Object> params) {
		if (groupCourse.getName() != null && !groupCourse.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + groupCourse.getName().trim() + "%");
		}

		if (groupCourse.getCoachName() != null && !groupCourse.getCoachName().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.TCoach.name like :value2 ";
			params.put("value2", "%" + groupCourse.getCoachName().trim() + "%");
		}
		if ((groupCourse.getBeginDatetime() != null && !groupCourse.getBeginDatetime().equals("")) && (groupCourse.getEndDatetime() != null && !groupCourse.getEndDatetime().equals(""))) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.beginDatetime between :value3 and :value4 ";
			params.put("value3", groupCourse.getBeginDatetime());
			params.put("value4", groupCourse.getEndDatetime());
		}
		return hql;
	}

	@Override
	public void save(GroupCourse groupCourse) {
		TGroupCourse t = new TGroupCourse();
		BeanUtils.copyProperties(groupCourse, t);
		t.setCdatetime(new Date());
		t.setTCoach(coachDao.get(TCoach.class, groupCourse.getPkCoach()));
		t.setTCourseDetail(courseDetailDao.get(TCourseDetail.class, groupCourse.getPkCourseDetail()));
		t.setTPlace(placeDao.get(TPlace.class, groupCourse.getPkPlace()));
		groupCourseDao.save(t);
	}

	@Override
	public void edit(GroupCourse groupCourse) {
		TGroupCourse t = new TGroupCourse();
		BeanUtils.copyProperties(groupCourse, t);
		t.setMdatetime(new Date());
		t.setTCoach(coachDao.get(TCoach.class, groupCourse.getPkCoach()));
		t.setTCourseDetail(courseDetailDao.get(TCourseDetail.class, groupCourse.getPkCourseDetail()));
		t.setTPlace(placeDao.get(TPlace.class, groupCourse.getPkPlace()));
		groupCourseDao.saveOrUpdate(t);
	}

	@Override
	public void remove(GroupCourse groupCourse) {
		TGroupCourse t = groupCourseDao.get(TGroupCourse.class, groupCourse.getPkGroupCourse());
		groupCourseDao.delete(t);
	}

}
